/*
 * @Description:
 * @Autor: dongjunhua
 * @Date: 2020-12-17 10:20:47
 * @LastEditors: dongjunhua
 * @LastEditTime: 2021-10-19 10:35:41
 */

/**Vue */
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import defaultConfig from './config';
/**style */
import 'normalize.css/normalize.css'; // a modern alternative to CSS resets
import '@/styles/index.scss'; // global css
/**ElementPlus */
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import 'dayjs/locale/zh-cn'; // for element zh-cn
import locale from 'element-plus/lib/locale/lang/zh-cn';
/**Vant */
import Vant from 'vant';
import 'vant/lib/index.css';
/**icons */
import './icons'; // icon
import SvgIcon from './components/SvgIcon/index.vue';
import '@fortawesome/fontawesome-free/css/all.css';
/**others */
import VueParticles from 'vue-particles';
/**micro app */
import { registerMicroApps } from 'qiankun';
import { microAppOptions, generateMicroAppOptions } from '@/router/micro-app';

let app = null;

function init(props = {}) {
  if (
    (process.env.NODE_ENV === 'production' && defaultConfig.useMock) ||
    (process.env.NODE_ENV === 'mock' && window.__POWERED_BY_QIANKUN__)
  ) {
    const { mockXHR } = require('../mock');
    mockXHR();
  }

  const { container } = props;

  app = createApp(App);

  // register plugins
  app
    .use(store)
    .use(router)
    .use(ElementPlus, { ...defaultConfig.element, locale })
    .use(Vant)
    .use(VueParticles);

  // register global component
  app.component('SvgIcon', SvgIcon);

  // mount instance
  app.mount(container ? container.querySelector('#app') : '#app');
}

// 独立运行
if (!window.__POWERED_BY_QIANKUN__) {
  if (microAppOptions && microAppOptions.length > 0) {
    registerMicroApps(generateMicroAppOptions(microAppOptions));
  }

  init();
}
// 以微应用方式运行时的钩子函数
export async function bootstrap() {
  console.log(`[${defaultConfig.applicationName}] micro app bootstraped`);
}
export async function mount(props) {
  console.log(`[${defaultConfig.applicationName}] micro app mount`);
  init(props);
}
export async function unmount() {
  console.log(`[${defaultConfig.applicationName}] micro app unmount`);
  app.unmount();
  app = null;
}
